package IOexpand;

import TEC.Tec;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;

import java.io.IOException;

public class IOExpand {


    private Tec tec;

    public Parent getIOExpand() throws IOException{
        Parent root;
//        try {
            root = FXMLLoader.load(getClass().getResource("/ui.fxml"));TextArea textArea1 = (TextArea) root.lookup("#textArea1");
            TextArea textArea2 = (TextArea) root.lookup("#textArea2");
            tec = new Tec(textArea1, textArea2);
            textArea1.appendText("Tec-XP Simulation Program\nVersion 1.0 2017.12.23\nComputer Architectur DUT\n>");
            textArea2.appendText("Tec-XP Simulation Program\nVersion 1.0 2017.12.23\nComputer Architectur DUT\n>");

            //设置成不可更改，通过键盘监听函数进行修改
            textArea1.setEditable(false);
            textArea2.setEditable(false);

            //键盘监听
            textArea1.setOnKeyPressed(new EventHandler<KeyEvent>() {
                @Override
                public void handle(javafx.scene.input.KeyEvent event) {
                    tec.whichScreen(event, 80);
                }
            });

            textArea2.setOnKeyPressed(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent event) {
                    tec.whichScreen(event, 90);
                }
            });

            //reset Start按钮绑定
            Button reset = (Button) root.lookup("#Reset");
            Button start = (Button) root.lookup("#Start");

            reset.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    tec.reset();
                }
            });

            start.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    tec.start();
                }
            });
//            return root;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return root;

}

    public IOExpand() {
    }
}
